package com.flycent.hqhs.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;

import com.flycent.hqhs.ui.ImagePagerActivity;

/**
 * @author fbl
 */
public class ImageUtil {


	/**
	 * 进入图片浏览
	 * @param startPos 进去显示的第一张图片
	 * @param urls  图片路径集
	 */
	public static void imageBrower(Context context,int startPos, String[] urls) {
		Intent intent = new Intent(context, ImagePagerActivity.class);
		// 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls);
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, startPos);
		context.startActivity(intent);
	}
	
	
	/** 
     * 得到本地或者网络上的bitmap url - 网络或者本地图片的绝对路径,比如: 
     *  
     * A.网络路径: url="http://blog.foreverlove.us/girl2.png" ; 
     *  
     * B.本地路径:url="file://mnt/sdcard/photo/image.png"; 
     *  
     * C.支持的图片格式 ,png, jpg,bmp,gif等等 
     *  
     * @param url  
     * @return BitMap
     */  
	public static Bitmap GetLocalOrNetBitmap(String url) {  
		Bitmap bitmap = null;  
		InputStream in = null;  
		BufferedOutputStream out = null;  
		try {  
			in = new BufferedInputStream(new URL(url).openStream(), 2*1024);  
			final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();  
			out = new BufferedOutputStream(dataStream, 2*1024);  
			copy(in, out);  
			out.flush();  
			byte[] data = dataStream.toByteArray();  
			bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);  
			data = null;  
			return bitmap;  
		}  
		catch (IOException e){  
			e.printStackTrace();  
			return null;  
		}  
	} 
	
	private static void copy(InputStream in, OutputStream out)
			throws IOException {
		byte[] b = new byte[2*1024];
		int read;
		while ((read = in.read(b)) != -1) {
			out.write(b, 0, read);
		}
	}
	
	//宽
	public static int getViewWidth(ImageView view){
	    view.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	    return view.getMeasuredWidth();
	}


}
